﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Labb2
{
    class CustomSystem
    {
        //private Texture2D _shockwaveTexture;
        //private Texture2D _sparkTexture;
        //private Texture2D _smokeTexture;
        private Vector2 _startingPosition;

        private float _shockwaveDiameter;

        private int _numberOfSmokes = 100;
        private int _numberOfSplitter = 50;

        SplitterParticle[] _splitterArray;
        Shockwave _shockwave;
        SmokeParticle[] _smokeArray;
        Camera _camera;

        public CustomSystem(Camera camera, Vector2 startingPosition) 
        {
            _camera = camera;
            _startingPosition = _camera.GetLogicalCoords(startingPosition);
            _splitterArray = new SplitterParticle[_numberOfSplitter];
            _smokeArray = new SmokeParticle[_numberOfSmokes];
            //_shockwaveDiameter = (int)_camera.GetVisualCoords(new Vector2(0.05f)).X;
            _shockwaveDiameter = 1f;
        }

        internal void Update(GameTime gameTime) 
        {
            foreach (SplitterParticle splitter in _splitterArray)
            {
                splitter.Update(gameTime);
            }
            foreach (SmokeParticle smoke in _smokeArray)
            {
                smoke.Update(gameTime);
            }
            _shockwave.Update(gameTime);
            //System.Diagnostics.Debug.WriteLine("Update");
        }
        
        internal void Draw(SpriteBatch spritebatch) 
        {
            _shockwave.Draw(spritebatch);
            foreach (SplitterParticle splitter in _splitterArray)
            {
                splitter.Draw(spritebatch);
            }
            foreach (SmokeParticle smoke in _smokeArray)
            {
                smoke.Draw(spritebatch);
            }
            //System.Diagnostics.Debug.WriteLine(_splitterArray.Length);
        }

        internal void LoadContent(ContentManager contManager) 
        {
            Texture2D shockwaveTexture = contManager.Load<Texture2D>("shockwave");
            Texture2D sparkTexture = contManager.Load<Texture2D>("spark");
            Texture2D smokeTexture = contManager.Load<Texture2D>("particlesmoke");

            for (int i = 0; i < _numberOfSplitter; i++) 
            {
                SplitterParticle splitter = new SplitterParticle(_startingPosition, i, sparkTexture, _camera);
                _splitterArray[i] = splitter;
            }
            for (int i = 0; i < _numberOfSmokes; i++)
            {
                SmokeParticle smoke = new SmokeParticle(_startingPosition, i, smokeTexture, _camera);
                _smokeArray[i] = smoke;
            }
            _shockwave = new Shockwave(_startingPosition, shockwaveTexture, _shockwaveDiameter, _camera);
            //System.Diagnostics.Debug.WriteLine("LoadContent");
        }
    }
}
